package com.luhuiguo.gwt.mvp.dispatcher.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.luhuiguo.gwt.mvp.dispatcher.server.Dispatcher;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Action;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Result;


/**
 * This class is the default implementation of {@link DispatcherAsync}, which is
 * essentially the client-side access to the {@link Dispatcher} class on the
 * server-side.
 *
 * @author LuHuiguo
 * @version $Id: StandardDispatcherAsync.java 11 2009-12-26 13:39:17Z luhuiguo $
 */
public class StandardDispatcherAsync extends AbstractDispatcherAsync {

    private static final StandardDispatcherServiceAsync realService = GWT.create( StandardDispatcherService.class );

    public StandardDispatcherAsync( ExceptionHandler exceptionHandler ) {
        super( exceptionHandler );
    }

    public <A extends Action<R>, R extends Result> void execute( final A action, final AsyncCallback<R> callback ) {
        realService.execute( action, new AsyncCallback<Result>() {
            public void onFailure( Throwable caught ) {
                StandardDispatcherAsync.this.onFailure( action, caught, callback );
            }

            @SuppressWarnings({"unchecked"})
            public void onSuccess( Result result ) {
                StandardDispatcherAsync.this.onSuccess( action, (R) result, callback );
            }
        } );
    }


}
